<!DOCTYPE html>
<html lang="zh">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>注册账号</title>
    <link rel="stylesheet" href="css/index.css">
    <style>
        .register-container {
            width: 400px;
            margin: 50px auto;
            padding: 20px;
        }

        .register-container h2 {
            text-align: center;
            margin-bottom: 30px;
        }

        .register-container label {
            display: inline-block;
            height: 35px;
            line-height: 35px;
        }

        .register-container input {
            width: 100%;
            height: 35px;
            margin: 0px;
            padding: 0px;
            margin-bottom: 15px;
        }

        .register-container button {
            width: 100%;
            height: 35px;
            margin-top: 20px;
        }

        .register-container .login-link {
            text-align: center;
            margin-top: 20px;
        }

        .register-container .login-link a {
            color: blue;
            text-decoration: none;
        }
    </style>
</head>

<body>
    <div class="register-container">
        <h2>注册新账号</h2>
        <div>
            <label for="username">用户名:</label>
            <input type="text" id="username" name="username" required>

            <label for="password">密码:</label>
            <input type="password" id="password" name="password" required>

            <label for="confirm-password">确认密码:</label>
            <input type="password" id="confirm-password" name="confirm-password" required>

            <button id="registerBtn">注册</button>

            <div class="login-link">
                已有账号？<a href="/">返回登录</a>
            </div>
        </div>
    </div>

    <script>
        document.getElementById('registerBtn').addEventListener('click', function () {
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const confirmPassword = document.getElementById('confirm-password').value;

            if (!username || !password || !confirmPassword) {
                alert('请填写所有字段');
                return;
            }

            if (password !== confirmPassword) {
                alert('两次输入的密码不一致');
                return;
            }

            fetch('/register', {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({
                    username: username,
                    password: password
                })
            })
                .then(response => response.json())
                .then(data => {
                    if (data.code === 200) {
                        alert('注册成功！');
                        window.location.href = '/';
                    } else {
                        alert(data.message);
                    }
                })
                .catch(error => {
                    alert('注册失败：' + error);
                });
        });
    </script>
</body>

</html>